package com.soft.mainsite.common.db.kit;

import com.soft.mainsite.common.db.impl.M;
import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.common.db.vo.FiledTypeVo;
import com.soft.mainsite.common.exception.SystemException;

/**
 * Description:cms 工具类
 *
 * @author Jin
 * @create 2017-05-12
 */
public class DbTableKit {

    public static final String[] MODEL_FILED_CLASS = {"input#单文本", "file#文件", "image#单图上传", "editor#编辑器", "textarea#多文本框", "radio#单选", "select#列表", "checkbox#多选", "dateInput#日期输入"};

    public static final String[] PREPARED_MODEL_FILED_NAME = {"title", "site_id", "siteId", "user_id", "userId", "category_id", "categoryId", "model_id", "modelId",
            "keywords", "description", "top", "recommend", "thumb", "status", "inputdate", "updatedate", "url", "author", "view_num", "viewNum", "static_filename", "staticFilename",
            "article_content", "articleContent", "image_name", "imageName", "image_url", "imageUrl", "video_name", "videoName", "video_url", "videoUrl", "site_type", "siteType"};

    private static final String INPUT = "input";
    private static final String FILE = "file";
    private static final String IMAGE = "image";
    private static final String EDITOR = "editor";
    private static final String TEXTAREA = "textarea";
    private static final String RADIO = "radio";
    private static final String CHECKBOX = "checkbox";
    private static final String SELECT = "select";
    private static final String DATE_INPUT = "dateInput";

    public static FiledTypeVo getFiledTypeVo(String filedClass, String filedType, Integer length, String defaultValue) {
        FiledTypeVo f = new FiledTypeVo();

        if (INPUT.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                f.setM(M.valueOf(filedType));
                f.setLength(length);
            } else {
                f.setM(M.VARCHAR_TYPE);
                f.setLength(255);
            }
            f.setDefaultValue(defaultValue);
        }

        if (FILE.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                f.setM(M.valueOf(filedType));
                f.setLength(length);
            } else {
                f.setM(M.VARCHAR_TYPE);
                f.setLength(128);
            }
            f.setDefaultValue(null);
        }

        if (IMAGE.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                f.setM(M.valueOf(filedType));
                f.setLength(length);
            } else {
                f.setM(M.TEXT_TYPE);
                f.setLength(null);
            }
            f.setDefaultValue(defaultValue);
        }

        if (EDITOR.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                if (MysqlFiledUtil.isAutoIncrementFiled(M.valueOf(filedType))) {
                    throw new SystemException("请选择非数字的字段类型类型！");
                }
                f.setM(M.valueOf(filedType));
                f.setLength(length);

            } else {
                f.setM(M.MEDIUM_TEXT_TYPE);
                f.setLength(null);
            }
            f.setDefaultValue(defaultValue);
        }

        if (TEXTAREA.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                if (MysqlFiledUtil.isAutoIncrementFiled(M.valueOf(filedType))) {
                    throw new SystemException("请选择非数字的字段类型类型！");
                }
                f.setM(M.valueOf(filedType));
                f.setLength(length);
            } else {
                f.setM(M.TEXT_TYPE);
                f.setLength(null);
            }
            f.setDefaultValue(defaultValue);
        }

        if (RADIO.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                f.setM(M.valueOf(filedType));
                f.setLength(length);
            } else {
                f.setM(M.VARCHAR_TYPE);
                f.setLength(200);
            }
            f.setDefaultValue(" ");
        }

        if (CHECKBOX.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                f.setM(M.valueOf(filedType));
                f.setLength(length);
            } else {
                f.setM(M.VARCHAR_TYPE);
                f.setLength(200);
            }
            f.setDefaultValue(" ");
        }

        if (SELECT.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                f.setM(M.valueOf(filedType));
                f.setLength(length);
            } else {
                f.setM(M.VARCHAR_TYPE);
                f.setLength(200);
            }
            f.setDefaultValue(null);
        }
        if (DATE_INPUT.equals(filedClass)) {
            if (!StrUtil.isBlank(filedType) && length != null) {
                f.setM(M.valueOf(filedType));
                f.setLength(length);
            } else {
                f.setM(M.CHAR_TYPE);
                f.setLength(32);
            }
            f.setDefaultValue(" ");
        }

        return f;
    }

}
